package com.stek.app;

import java.util.List;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.TextView;

import com.stek.R;
import com.stek.lib.StickyGridHeadersAdapter;
import com.stek.model.AppInfo;
import com.stek.utils.Utils;

public class AppGridAdapter extends StickyGridHeadersAdapter<AppInfo> {

	public AppGridAdapter(Context context, List<AppInfo> items) {
		super(context, items);
	}

	@Override
	public String getHeaderField(int position) {
		return ((AppInfo) mItems.get(position)).name.substring(0, 1);
	}

	@Override
	public View getHeaderView(int position, View convertView, ViewGroup parent) {
		HeaderHolder holder;
		if (null == convertView) {
			convertView = mInflater.inflate(R.layout.row_header, null);
			holder = new HeaderHolder();
			holder.tvTitle = (TextView) convertView.findViewById(R.id.tv_title);
			convertView.setTag(holder);
		} else {
			holder = (HeaderHolder) convertView.getTag();
		}
		holder.tvTitle.setText(getItem(position).name.substring(0, 1));
		return convertView;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		ViewHolder holder;
		if (null == convertView) {
			convertView = mInflater.inflate(R.layout.view_grid1, null);
			holder = new ViewHolder();
			holder.tvTitle = (TextView) convertView.findViewById(R.id.tv_title);
			holder.imgIcon = (ImageView) convertView.findViewById(R.id.img);
			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}
		holder.tvTitle.setText(getItem(position).name);
		holder.imgIcon.setImageDrawable(getItem(position).icon);
		Utils.animateGridView(convertView, ((GridView) parent).getNumColumns(),
				position);
		return convertView;
	}

	static class HeaderHolder {
		TextView tvTitle;
	}

	static class ViewHolder {
		TextView tvTitle;
		ImageView imgIcon;
	}

}
